import 'package:flutter/material.dart';
import 'package:flutter_create_framework/generated/l10n.dart';
import 'package:flutter_create_framework/provider/state_provider.dart';
import 'package:flutter_create_framework/util/location_storge_util.dart';
import 'package:provider/provider.dart';

class SettingLanguage extends StatefulWidget {
  @override
  _SettingLanguageState createState() => _SettingLanguageState();
}

class _SettingLanguageState extends State<SettingLanguage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(S.of(context).language),
      ),
      body: ListView(
        physics: BouncingScrollPhysics(),
        children: <Widget>[
          ListTile(
            title: Text("简体中文",style: TextStyle(color: Colors.black),),
            trailing: Visibility(child: Icon(Icons.check),visible: context.watch<StateProvider>().language=="简体中文",),
            onTap: ()async{
              context.read<StateProvider>().setLanguage("简体中文");
              await LocationStrogeUtil().set("language", "zh");
              await S.load(Locale("zh"));
              Navigator.of(context).pushNamedAndRemoveUntil("/", (route) => false);
            },
          ),
          ListTile(
            title: Text("English",style: TextStyle(color: Colors.black),),
            trailing: Visibility(child: Icon(Icons.check),visible: context.watch<StateProvider>().language=="English",),
            onTap: ()async{
              context.read<StateProvider>().setLanguage("English");
              await LocationStrogeUtil().set("language", "en");
              await S.load(Locale("en"));
              Navigator.of(context).pushNamedAndRemoveUntil("/", (route) => false);
            },
          ),
        ],
      ),
    );
  }
}
